// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/enterprise/base");
	var router = require("../hash/pageRouter");
	var footer = require("../footer/footer_gray");
	var top = require("../top/top_main");
	var constant = require("constant");
	var databind = require("databind");
	var dialog = require("dialog");
	var css_bill = require("./myBill.css");
	var flag = false;
	var html_receivables_detail = require("./html_receivables_detail.html");
	var html_loan_detail = require("./html_loan_detail.html");
	var html_expense_detail = require("./html_expense_detail.html");
	var css_dialog = require("css_dialog");
	var tools = require("tools");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var oData = {
		pageIndex: pageIndex,
		pageSize: pageSize,
		totalPageCount: totalPageCount,
		totalRecordCount: totalRecordCount,
		statementBeginDate: "",
		statementEndDate: "",
		statementNumber: "",
		receiptName: "",
		minSourceAmount: "",
		maxSourceAmount: "",
		financeNumber: ""
	};
	var init = function() {
		inquireMoneyStatement();
		router.init();
		initEvent();
	};

	var initEvent = function() {
		if (base.getData("sid") == null) {
			location.href = "../login/login.html";
		};

		$("#disbursementSearch").unbind("click").click(function() {
			oData = {
				pageIndex: pageIndex,
				pageSize: pageSize,
				totalPageCount: totalPageCount,
				totalRecordCount: totalRecordCount,
				statementBeginDate: $("#myBill .statementBeginDate").val(),
				statementEndDate: $("#myBill .statementEndDate").val(),
				statementNumber: $("#myBill .statementNumber").val(),
				receiptName: $("#myBill .receiptName").val(),
				minSourceAmount: $("#myBill .minSourceAmount").val(),
				maxSourceAmount: $("#myBill .maxSourceAmount").val(),
				financeNumber: $("#myBill .financeNumber").val()
			};
			tools.loadList("inquireDisbursementMoneyStatementPageList.json", $("#disbursementManageBody"), $("#disbursementManageTpl"), oData, function() {
				detailEven();

				$(".verify").unbind("click").click(function() {
					var $this = $(this)
					dialog.showConfirmDialog("核销", "是否确定核销该条账单？", function() {
						verifiCancelMoneyStatement($this.parents("tr").attr("moneystatementId"));
					});
				});
			});
		});

		

	};

	var inquireMoneyStatement = function() {
		tools.loadList("inquireDisbursementMoneyStatementPageList.json", $("#disbursementManageBody"), $("#disbursementManageTpl"), oData, function() {
			detailEven();

			$(".applicationDetail").unbind("click").click(function(){
				router.route("account/accountDetail.html?financeId=" + $(this).parents("tr").attr("financeId"));
			});
		});
	};
	
	var detailEven = function(){
		$("#myBill .detail").unbind("click").click(function() {
			var statementSourceId = $(this).parents("tr").attr("statementSourceId");
			var statementSource = $(this).parents("tr").attr("statementSource");
			switch (statementSource) {
				case "RECEIVABLES":
//					$("#popdialog").html(html_receivables_detail);
//					dialog.showDialog($("#receivable_detail_dialog"));
//					databind.fillInput($("#receivable_detail_dialog"), {
//						action: "inquireReceivablesDetail.json?receivablesId=" + statementSourceId
//					});
//					databind.disableInput($("#receivable_detail_dialog"));
//					$(".sure_yes").unbind("click").click(function() {
//						dialog.hideDialog($("#receivable_detail_dialog"));
//					});
//
//					$(".sure_no").unbind("click").click(function() {
//						router.route("moneyStatementManage/receivablesNotice.html.html?receivablesId=" + statementSourceId)
//						dialog.hideDialog($("#receivable_detail_dialog"));
//					});
					router.route("account/accountReceivableDetail.html?pickupId="+statementSourceId);
					break;
				case "ABNORMAL_EXPENSE":
					$("#popdialog").html(html_expense_detail);
					dialog.showDialog($("#exception_detail_dialog"));
					tools.inquireCommonSelectorList(9, $(".expenseType"));
					databind.fillInput($("#exception_detail_dialog"), {
						action: "inquireAbnormalExpenseDetail.json?abnormalExpenseId=" + statementSourceId
					});
					databind.disableInput($("#exception_detail_dialog"));
					$(".sure_yes").unbind("click").click(function() {
						dialog.hideDialog($("#exception_detail_dialog"));
					});
					$(".sure_no").unbind("click").click(function() {
						var abnormalId = $("#abnormalId").val();
						dialog.hideDialog($("#exception_detail_dialog"));
						storage.setItem("abnormalId", abnormalId);
						location.href = "../exception/exceptionManage.html#exception/exceptionDetailInfo";
					});
					break;
				case "LOAN_NOTICE":
					$("#popdialog").html(html_loan_detail);
					dialog.showDialog($("#loan_detail_dialog"));
					databind.fillInput($("#loan_detail_dialog"), {
						action: "inquireLoanNoticeDetail.json?loanNoticeId=" + statementSourceId
					});
					databind.disableInput($("#loan_detail_dialog"));
					$(".sure_yes").unbind("click").click(function() {
						dialog.hideDialog($("#loan_detail_dialog"));
					});

					$(".sure_no").unbind("click").click(function() {
						router.route("moneyStatementManage/loanNotice.html?loanNoticeId=" + statementSourceId)
						dialog.hideDialog($("#loan_detail_dialog"));
					});
					break;
				case "FINANCE_AUDIT":
					router.route("account/financeApproveDetail.html.html?financeAuditId=" + statementSourceId)
					break;
				default:
					break;
			}
		});
	}

	return {
		init: init
	};
});